import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

// 路由配置
const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../components/UserLogin.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('../components/Dashboard.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/upload',
    name: 'DataUpload',
    component: () => import('../components/DataUpload.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/report',
    name: 'TaxReport',
    component: () => import('../components/TaxReport.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/logs',
    name: 'SystemLogs',
    component: () => import('../components/SystemLogs.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/users',
    name: 'UserManagement',
    component: () => import('../components/UserManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/user-roles',
    name: 'UserRoleManagement',
    component: () => import('../components/UserRoleManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/roles',
    name: 'RoleManagement',
    component: () => import('../components/RoleManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/permissions',
    name: 'PermissionManagement',
    component: () => import('../components/PermissionManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/menus',
    name: 'MenuManagement',
    component: () => import('../components/MenuManagement.vue'),
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/tax-management',
    name: 'TaxManagement',
    component: () => import('../components/TaxManagement.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/tax-identity',
    name: 'TaxIdentityManagement',
    component: () => import('../components/TaxIdentityManagement.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/tax-income',
    name: 'TaxIncomeManagement',
    component: () => import('../components/TaxIncomeManagement.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/tax-platform',
    name: 'TaxPlatformManagement',
    component: () => import('../components/TaxPlatformManagement.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'UserProfile',
    component: () => import('../components/UserProfile.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 从localStorage检查登录状态，避免在路由守卫中直接使用store
  const token = localStorage.getItem('token')
  const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
  const isLoggedIn = !!token
  
  console.log('路由守卫检查:', { to: to.path, userInfo, isLoggedIn })
  
  // 检查是否需要认证
  if (to.meta.requiresAuth && !isLoggedIn) {
    console.log('需要认证，重定向到登录页')
    next('/login')
    return
  }
  
  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin) {
    const userRole = userInfo.role || userInfo.roles?.[0] || ''
    const hasAdminRole = ['ADMIN', 'SUPER_ADMIN'].includes(userRole)
    
    console.log('检查管理员权限:', { userRole, hasAdminRole, requiresAdmin: to.meta.requiresAdmin })
    
    if (!hasAdminRole) {
      console.log('权限不足，重定向到主面板')
      next('/dashboard')
      return
    }
  }
  
  // 如果已登录且访问登录页，重定向到主面板
  if (to.path === '/login' && isLoggedIn) {
    next('/dashboard')
    return
  }
  
  console.log('路由检查通过，允许访问:', to.path)
  next()
})

export default router
